package cn.ssm.controller;



import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.ssm.biz.impl.ClassroomBizImpl;
import cn.ssm.biz.impl.StudentBizimpl;
import cn.ssm.pojo.Class;
import cn.ssm.pojo.Pager;
import cn.ssm.pojo.Student;
import cn.ssm.query.StudentQuery;
/**
 * 测试二位
 * @author hkl 
 * 
 */
@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	@Qualifier("studentBiz")
	private StudentBizimpl studentBizimpl;
	
	@Autowired
	private ClassroomBizImpl classroomBizImpl;

	
	
	
	@RequestMapping("/finStudent")
	public String finStudent(ModelMap modelMap,HttpServletRequest request,@RequestParam("id") Integer id){
		
		String currentPageStr =request.getParameter("currentPage");
		System.out.println(currentPageStr+"======="+id);
		int rowCount = studentBizimpl.findStuCountByClassId(id);
		Pager pager=null;
		if(currentPageStr==null){
			pager=new Pager(rowCount, 5, 1);
		}else{
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			pager=new Pager(rowCount, 5, currentPage);
		}
		
		
		pager.setParams("?id="+id);	
		pager.setUrl("student/finStudent");
		List<StudentQuery> studentList =studentBizimpl.selectStudentlistByClassId(id, pager);
		
		System.out.println("studentList====="+studentList.size());
		
		
		modelMap.put("studentList", studentList);
		modelMap.put("pager", pager);
		modelMap.put("id", id);
		
		return "StudentList";
		
	}
	
	@RequestMapping("/finStudentAll")
	public String finStudentAll(ModelMap modelMap){
		
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		System.out.println("classroomInfo===="+classroomInfo.size());
		modelMap.put("classroomInfo", classroomInfo);
		
		
		return "studentAll";
	}
	
	
	@RequestMapping("/findOne")
	public String findOne(int id,ModelMap modelMap){
		
		Student stu =studentBizimpl.findStuById(id);

		String url=" ";
//		if(stu != null ){
//			modelMap.addObject("stu", stu);
		
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		modelMap.put("classroomInfo",classroomInfo);
		modelMap.put("stu", stu);
			url="updateStudent";
//		}else{
//			url="404";
//		}
		return url;
	}
	
	
	@RequestMapping("/delStudentAll")
	public String delStudentAll(int id,ModelMap modelMap,Integer classroomId){
		int num=studentBizimpl.delStudent(id);
		String url=" ";
		if(num > 0){
			//modelMap.remove("studentList");
			url="forward:/student/finStudent?id="+classroomId;
//			url="studentAll";
//			url="forward:student/finStudent";
		}else{
			url="404";
		}
		return url;
	} 
	
	@RequestMapping("/update")
	public String update(Student s,ModelMap modelMap,Integer classroomId){
		
		int num=studentBizimpl.UpdateStudent(s);
		String url=" ";
		if(num > 0){
			//modelMap.remove("studentList");
			url="forward:/student/finStudent?id="+classroomId;
//			url="studentAll";
		}else{
			url="404";
		}
		return url;
	}
	
	@RequestMapping("/addFindList")
	public String addFindList(ModelMap modelMap){
		
		List<Class> classroomInfo = classroomBizImpl.classroomInfo1();
		System.out.println("==================="+classroomInfo.size());
		
		modelMap.put("classroomInfo",classroomInfo);
		
		return "addStudent";
	}	
	@RequestMapping("/add")
	public String add(Student s,ModelMap modelMap,HttpServletRequest request,@RequestParam("computerId") int computerId,@RequestParam("classroomId") int classroomId){
		int computerroomId=classroomBizImpl.getcomputerroomByClassId(classroomId);
		s.setClasssId(classroomId);
		s.setConputerroomId(computerroomId);
		int num=studentBizimpl.addStudent(s);
		System.out.println("classid======================"+classroomId);
		System.out.println("computerid============================"+computerId);
		

		
		String url=" ";
		if(num > 0){
		
			url="forward:/student/finStudent?id="+classroomId;
		}else{
			url="404";
		}
		return url;
	} 


}
